package com.activities;


import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.Animations.AnimUtil;
import com.Data.Follow;
import com.Data.Follow.onGetFollowCompleted;
import com.Data.SocialInfo;
import com.Data.SocialInfo.onGetSocialInfoCompleted;
import com.Data.User;
import com.Data.User.onGetUserInfoCompleted;
import com.Downloaders.ProfilePictureDownloader;
import com.Downloaders.ProfilePictureDownloader.onProfilePictureDownloaded;
import com.Downloaders.ThumbDownloader;
import com.Downloaders.ThumbDownloader.ThumbnailPathCompleted;
import com.Enums.BlurType;
import com.Listeners.FollowButtonListener.AddFollowerListener;
import com.Listeners.FollowButtonListener.DeleteFollowerListener;
import com.Managers.PictureManagerClass.CreateBlurAsync;
import com.Managers.ThemeManager;
import com.Managers.UserManagerClass;
import com.TextFormat.UtilFonts;
import com.UtilDate.UtilDate;
import com.UtilDate.UtilDate.OnSetDifferenceCompleted;
import com.shareup.R;


public class ActivityProfileUser extends Activity {

    public static final String KEY_ID = "IDUSER";

    public static final String KEY_NAME = "NAMEUSER";

    public static void delayTask(final User task) {

        Handler myHandler = new Handler();
        myHandler.postDelayed(new Runnable() {

            @Override
            public void run() {

                task.execute(User.ACTION_GET_INFO);
            }
            //
        }, 1000);
    }

    private ImageView coverLayout;

    private TextView desc;

    private ImageView followButton;

    private TextView followers;

    private TextView followersCounter;

    private TextView following;

    private TextView followingCounter;

    private TextView lastSeenDate;

    private TextView pictures;

    private TextView picturesCounter;

    private ImageView profileImage;

    private ProgressBar progress;

    private RelativeLayout socialInfo;

    private User user;

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            //
            return true;
        }
        //
        return super.onKeyUp(keyCode, event);
    }

    public void updateFollowInfo(final int id) {

        User us = new User();

        us.setID(id);
        us.setCallbackGetInfo(new onGetUserInfoCompleted() {

            @Override
            public void Completed(User result) {

                user = result;
                setFollowInfo();
                setProfileInfo(id);
            }
        });
        us.execute(User.ACTION_GET_INFO);
    }

    private void downloadCover(String path) {

        ThumbDownloader td = new ThumbDownloader(this, path);
        td.setCompletePath(new ThumbnailPathCompleted() {

            @Override
            public void Completed(String path) {

                int Blur = BlurType.BLUR_COVER.getValue();
             
                if (user.getBlur() == 0)
                    Blur = 0;

                CreateBlurAsync cbs = new CreateBlurAsync(Blur, coverLayout, path);
                cbs.setUserProfile(true);
                cbs.execute();

            }
        });
        td.execute();

    }

    private void exit() {

        finish();
        AnimUtil.getActivityTransition(this, false);
    }

    private void getUserInfo(int id) {

        User us;

        if (id == UserManagerClass.UserLogged.getID()) {
            us = UserManagerClass.UserLogged;
            user = us;
            setInfo();
        }
        else {
            us = new User();
            us.setID(id);
            us.setCallbackGetInfo(new onGetUserInfoCompleted() {

                @Override
                public void Completed(User result) {

                    user = result;
                    setInfo();
                }
            });

            delayTask(us);
        }

    }

    private void setFollowersListener(Boolean result, User user) {

        if (result) {
            AnimUtil.animateFadeIn(followButton);
            followButton.setOnClickListener(new DeleteFollowerListener(user.getID(), followButton, this));
        }
        else
            if (user.getPrivacy() == 0) {
                followButton.setVisibility(View.INVISIBLE);
                followButton.setOnClickListener(new AddFollowerListener(user.getID(), followButton, this));
            }
            else {
                if (followButton.getVisibility() == View.INVISIBLE)
                    AnimUtil.animateFadeIn(followButton);

                followButton.setOnClickListener(new AddFollowerListener(user.getID(), followButton, this));
            }

    }

    private void setFollowInfo() {

        if (user.getID() != UserManagerClass.UserLogged.getID()) {

            Follow fl = new Follow(user.getID());
            fl.setCallbackGetFollow(new onGetFollowCompleted() {

                @Override
                public void Completed(Boolean result) {

                    setFollowersListener(result, user);
                }
            });
            fl.execute(Follow.ACTION_CHECK);
        }
    }

    private void setInfo() {

        setFollowInfo();
        setProfile(this.user);

    }

    private void setProfile(final User user) {

        profileImage = (ImageView) this.findViewById(R.id.aprofile_edit_image);
        progress = (ProgressBar) this.findViewById(R.id.loading_progress);
        //
        TextView namep = (TextView) this.findViewById(R.id.dialog_profile_name);
        namep.setText(user.getRealName());
        //
        followers = (TextView) this.findViewById(R.id.aprofile_followers);
        pictures = (TextView) this.findViewById(R.id.aprofile_pictures);
        following = (TextView) this.findViewById(R.id.aprofile_following);
        lastSeenDate = (TextView) this.findViewById(R.id.profile_lastseen);
        lastSeenDate.setVisibility(View.INVISIBLE);
        desc = (TextView) this.findViewById(R.id.aprofile_description);
        //
        picturesCounter = (TextView) this.findViewById(R.id.profile_p_counter);
        followersCounter = (TextView) this.findViewById(R.id.profile_e_counter);
        followingCounter = (TextView) this.findViewById(R.id.profile_f_counter);

        //
        UtilFonts.setTextRegularFont(picturesCounter);
        UtilFonts.setTextRegularFont(followingCounter);
        UtilFonts.setTextRegularFont(followersCounter);
        UtilFonts.setTextRegularFont(lastSeenDate);
        //
        UtilFonts.setTextRegularFont(following);
        UtilFonts.setTextRegularFont(pictures);
        UtilFonts.setTextRegularFont(followers);
        UtilFonts.setTextRegularFont(desc, true);
        UtilFonts.setTextBoldFont(namep);

        //
        ProfilePictureDownloader pd = new ProfilePictureDownloader();
        pd.setProgress(progress);
        pd.setCallbackProfilePicture(new onProfilePictureDownloaded() {

            @Override
            public void completed(Bitmap result) {

                setProfileImage(result, user.getCoverPicture());

            }
        });

        pd.execute(user.getProfilePicture());
        //
        setProfileInfo(user.getID());

    }

    private void setProfileImage(Bitmap image, String path) {

        this.profileImage.setImageBitmap(image);

        if (!path.equals("null"))
            downloadCover(path);

    }

    private void setProfileInfo(int id) {

        SocialInfo sinfo = new SocialInfo();
        sinfo.setIDUser(id);
        sinfo.setCallbackGetSocialInfo(new onGetSocialInfoCompleted() {

            @Override
            public void Completed(SocialInfo result) {

                setSocialInfo(result);

            }
        });

        sinfo.execute();
    }

    private void setSocialInfo(final SocialInfo info) {

        String f = getString(R.string.profile_followers);
        String ll = getString(R.string.profile_followings);
        String p = getString(R.string.profile_pictures);
        String d = info.getDescription();
        new UtilDate(this).setTimeDifference(info.getLastSeen(), new OnSetDifferenceCompleted() {

            @Override
            public void Completed(String diff) {

                lastSeenDate.setText(diff);
                // AnimUtil.animateFadeIn(lastSeenDate);

            }
        });
        //
        this.followers.setText(f);
        this.following.setText(ll);
        this.pictures.setText(p);
        if (!d.equals("null"))
            this.desc.setText(d);
        //
        this.picturesCounter.setText(String.valueOf(info.getPictures()));
        this.followersCounter.setText(String.valueOf(info.getFollowers()));
        this.followingCounter.setText(String.valueOf(info.getFollowing()));
        //
        if (!d.equals("null")) {
            AnimUtil.animateFadeIn(desc);
        }
        AnimUtil.animateFadeIn(socialInfo);

    }

    //
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        
        setContentView(R.layout.activity_profile);
        super.onCreate(savedInstanceState);
        if (UserManagerClass.UserLogged == null)
            UserManagerClass.UserLogged = new UserManagerClass(this).getInfo();
        //
        socialInfo = (RelativeLayout) findViewById(R.id.table_info);
        socialInfo.setVisibility(View.INVISIBLE);
        coverLayout = (ImageView) findViewById(R.id.coverView);
        followButton = (ImageView) findViewById(R.id.profile_follow_button);
        followButton.setVisibility(View.INVISIBLE);
        //
        Bundle extras = getIntent().getExtras();
        //
        int IDUSER = 0;
        //

        if (extras != null) {
            IDUSER = extras.getInt(KEY_ID);
         
        }
        else {
            IDUSER = UserManagerClass.UserLogged.getID();
           
        }

     

        ImageView backB = (ImageView) findViewById(R.id.button_back);
        backB.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                exit();
            }
        });

        getUserInfo(IDUSER);
    }
}
